package com.glorysoft.hlgdpda.ui.materialandtool.view

import android.os.Bundle
import android.view.View
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseActivity
import com.glorysoft.hlgdpda.databinding.ActivityToolBindBinding
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.ui.materialandtool.viewmodel.MaterialViewModel
import com.glorysoft.hlgdpda.utils.PopWindowsUtils
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils
import kotlinx.android.synthetic.main.activity_tool_bind.etEqpWorkID
import kotlinx.android.synthetic.main.activity_tool_bind.flPositionList
import kotlinx.android.synthetic.main.activity_tool_bind.ivPositionArrow
import kotlinx.android.synthetic.main.activity_tool_bind.tvPosition

/**
 * FileName: MaterialUpActivity
 * Author: XJH
 * Date: 2021/5/14 17:48
 * Description:
 * History:
 */
class ToolBindActivity : BaseActivity<MaterialViewModel, ActivityToolBindBinding>(),
    View.OnClickListener {

    private var positionPop: PopWindowsUtils.GeneralPop? = null

    override fun layoutId(): Int {
        return R.layout.activity_tool_bind
    }

    override fun initVariableId(): Int {
        return BR.materialViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("Tool绑定设备")
        viewModel.initViewModel()
        flPositionList.setOnClickListener(this)
        setRightListener("绑定") {
            viewModel.bindTool()
        }
    }

    override fun initData() {
        initEdit(etEqpWorkID, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.getPositionName()
            }

        })
    }

    override fun onClick(v: View?) {
        if (viewModel.positionNameList.size <= 0) {
            viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到设备位置信息", false)
            return
        }

        if (positionPop == null) {
            positionPop = PopWindowsUtils.getOneChoosePop(
                this,
                flPositionList.width,
                viewModel.positionNameList,
                ivPositionArrow,
                object : PopWindowsUtils.PopTextAndPositionCallback {
                    override fun onSuccess(codeS: String, potion: Int) {
                        tvPosition.text = codeS
                        viewModel.positionName = codeS.split("/")[0]
                    }

                })

        }

        if (positionPop!!.isShowing) {
            positionPop?.dismiss()
        } else {
            PopWindowsUtils.setArrowShow(ivPositionArrow)
            positionPop?.showAsDropDown(flPositionList, 0, 0)
        }
    }
}